/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bddfirstproject.view;

import bddfirstproject.MainController;
import bddfirstproject.controller.PartieRedactionDAL;
import bddfirstproject.model.PartieRedaction;
import bddfirstproject.view.renderer.ArticleJTree;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.*;
import javax.swing.tree.*;

/**
 *
 * @author epulapp
 */
public class TreeDocumentsView extends NamedJPanel {
    protected final static String title = "Arbre de l'article";
    
    protected ArticleJTree treeObject;
    protected DefaultMutableTreeNode treeDatas;
    protected DefaultTreeModel treeModel;
    
    /**
     * Creates new form MainPanel
     */
    public TreeDocumentsView() {
        initComponents();
        
        // Création de l'arbre
        this.treeObject = new ArticleJTree();
        this.treeScrollPane.setViewportView(treeObject);
        this.treeScrollPane.revalidate();
        this.treeScrollPane.repaint();
        
        // Article courant
        MainController mainController = MainController.getInstance();
        PartieRedaction rootPartieRedaction = mainController.getCurrentArticle();
        ArrayList<PartieRedaction> partiesList = PartieRedactionDAL.getAllSousPartiesArticle(rootPartieRedaction.getIdPartie());
        this.labelArticleName.setText(rootPartieRedaction.getTitrePartie());
        
        // Construction de la liste des noeuds
        int rootNode = -1;
        DefaultMutableTreeNode currentNode;
        HashMap<Integer, DefaultMutableTreeNode> nodes = new HashMap<Integer, DefaultMutableTreeNode>();
        System.out.println("Parcour des " + partiesList.size() + " parties");
        for(PartieRedaction partie : partiesList) {
            currentNode = new DefaultMutableTreeNode(partie);
            nodes.put(partie.getIdPartie(), currentNode);
        }
        
        // Association des noeuds enfants/parents
        for(int idPartie : nodes.keySet()) {            
            currentNode = (DefaultMutableTreeNode) nodes.get(idPartie);
            PartieRedaction partie = (PartieRedaction) currentNode.getUserObject();
            System.out.println("Analyse de la hierarchie de la partie " + idPartie + " fille de " + partie.getIdInclusionDansPartie() + "...");
            
            if(partie.getIdInclusionDansPartie() < 0) {
                rootNode = idPartie;
                System.out.println("Noeud racine trouve : " + idPartie);
            } else if(nodes.keySet().contains(partie.getIdInclusionDansPartie())) {
                // Si le noeud parent existe dans la liste des nodes, on inclut le node courant dans ce node parent
                nodes.get(partie.getIdInclusionDansPartie()).add(currentNode);
                
                System.out.println("Parent de " + idPartie + " dans " + partie.getIdInclusionDansPartie());
            }
        }
         
        if(rootNode > 0) {
            System.out.println("Chargement de l'arbre avec le noeud racine " + rootNode);
            
            // Mise en attribut de l'arbre de données
            this.treeDatas = nodes.get(rootNode);
            this.treeModel = new DefaultTreeModel(treeDatas);
            this.treeObject.setModel(treeModel);
        }
        
    }

    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jLabel1 = new javax.swing.JLabel();
        labelArticleName = new javax.swing.JLabel();
        treeScrollPane = new javax.swing.JScrollPane();
        btnRetourTree = new javax.swing.JButton();
        btnEditerArticle = new javax.swing.JButton();

        jScrollPane1.setViewportView(jTree1);

        jLabel1.setText("Liste des parties pour le l'article ");

        labelArticleName.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelArticleName.setText("jLabel2");

        btnRetourTree.setText("Retour à la liste des article");
        btnRetourTree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRetourTreeActionPerformed(evt);
            }
        });

        btnEditerArticle.setText("Editer la partie");
        btnEditerArticle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditerArticleActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(treeScrollPane)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnRetourTree)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 65, Short.MAX_VALUE)
                        .addComponent(btnEditerArticle, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelArticleName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(labelArticleName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(treeScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRetourTree)
                    .addComponent(btnEditerArticle))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditerArticleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditerArticleActionPerformed
        MainController mainController = MainController.getInstance();
        DefaultMutableTreeNode noeudCourant = (DefaultMutableTreeNode) treeObject.getLastSelectedPathComponent();
        PartieRedaction partieEdition = (PartieRedaction) noeudCourant.getUserObject();
        System.out.println("Selection de l'article " + partieEdition);
        mainController.setCurrentPartieEdition(partieEdition);
        mainController.setCurrentView(new EditionPartieArticleView());
    }//GEN-LAST:event_btnEditerArticleActionPerformed

    private void btnRetourTreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRetourTreeActionPerformed
        MainController mainController = MainController.getInstance();
        mainController.setCurrentArticle(null);
        mainController.setCurrentView(new ArticlesView());
    }//GEN-LAST:event_btnRetourTreeActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEditerArticle;
    private javax.swing.JButton btnRetourTree;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    private javax.swing.JLabel labelArticleName;
    private javax.swing.JScrollPane treeScrollPane;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getTitle() {
        return title;
    }
}
